package com.yanqu.road.server.manger.activity.unionhuaben;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UnionHuaBenActivityMgr extends TempMgr {

    static ActivityInfo activityInfo = null;

    static String UNION_STORY_DRAW_REWARD = null;

    //关扑奖励是否公告标识
    static List<Integer> guanPuRewardNoticeList = new ArrayList<>();


    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return false;
    }

    @Override
    public boolean reloadData() throws Exception {
        return false;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {

        getLogger().info("reload huaBen Activity start");

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.UnionHuaBenActivity.getValue(), 1);
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no huaBen Activity in show time");
            activityInfo = null;

        } else {
            activityInfo = openActivityInfoList.get(0);
        }

        if(activityInfo != null){
            // config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            if(activityConfigMap != null){
                if(activityConfigMap.containsKey(eActivityConfigType.UNION_STORY_DRAW_REWARD.getName())){
                    UNION_STORY_DRAW_REWARD = activityConfigMap.get(eActivityConfigType.UNION_STORY_DRAW_REWARD.getName()).getValue();
                }
                if(activityConfigMap.containsKey(eActivityConfigType.UNION_STORY_DRAW_REWARD_NOTICE.getName())){
                    guanPuRewardNoticeList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.UNION_STORY_DRAW_REWARD_NOTICE.getName()).getValue(), "\\|");
                }
            }
        }

        getLogger().info("reload huaBen Activity end");
    }

    public static boolean isRewardIdxNeedNotice(int idx){
        if(idx + 1 > guanPuRewardNoticeList.size()){
            return false;
        }
        return guanPuRewardNoticeList.get(idx) == 1;
    }

    public static HuaBenGuanPuResult randomReward(){
        HuaBenGuanPuResult result = new HuaBenGuanPuResult("", 0);
        if(activityInfo == null){
            return result;
        }
        List<String> itemStr = StringUtils.stringToStringList(UNION_STORY_DRAW_REWARD, "\\|");
        List<Integer> weightList = new ArrayList<>();
        List<String> rewardList = new ArrayList<>();

        for (String sItem : itemStr) {
            List<String> valueList = StringUtils.stringToStringList(sItem, ";");
            weightList.add(Integer.valueOf(valueList.get(1)));
            rewardList.add(valueList.get(0));
        }

        int idx = RandomHelper.getRandomIndexByWeight(weightList);
        result.setReward(rewardList.get(idx));
        result.setIdx(idx);
        return result;
    }


    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }
}
